/*文档加载执行事件*/
$(document).ready(function() {
	$('.headline ul li').click(function() {
		$(this).css('background', '#ffffff');
		$(this).siblings().css('background', '#e6f1ea');
	});
	$('.headline ul li').click(function() {
		$("input[type='checkbox']").removeAttr('checked')
		$('#subject').val($(this).attr('subject'))
		$('#place').val($('#' + $(this).attr('place')).val());
	});
})

/*页面加载后执行*/
$(function() {
	var mydate = new Date();
	var h = mydate.getHours();
	var m = mydate.getMinutes();
	var time = h + m / 60;
	if(time >= 7 && time < 8) {
		$('.t1').html('<p>关闭</p>');
	} else if(time >= 8.33 && time <= 9.33) {
		for(var i = 1; i <= 2; i++) {
			$('.t' + i).html('<p>关闭</p>');
		}
	} else if(time >= 9.66 && time <= 10.66) {
		for(var i = 1; i <= 3; i++) {
			$('.t' + i).html('<span>关闭</span>');
		}
	} else if(time >= 11 && time <= 12) {
		for(var i = 1; i <= 4; i++) {
			$('.t' + i).html('<span>关闭</span>');
		}
	} else if(time >= 12.33 && time <= 13.33) {
		for(var i = 1; i <= 5; i++) {
			$('.t' + i).html('<span>关闭</span>');
		}
	} else if(time >= 13.66 && time <= 14.66) {
		for(var i = 1; i <= 6; i++) {
			$('.t' + i).html('<span>关闭</span>');
		}
	} else if(time >= 15 && time <= 16) {
		for(var i = 1; i <= 7; i++) {
			$('.t' + i).html('<span>关闭</span>');
		}
	} else if(time >= 16.33 && time <= 17.33) {
		for(var i = 1; i <= 8; i++) {
			$('.t' + i).html('<span>关闭</span>');
		}
	} else if(time >= 17.66) {
		for(var i = 1; i <= 9; i++) {
			$('.t' + i).html('<span>关闭</span>');

		}
	}

	var url = localStorage.icandriveUrl;
	var token = localStorage.phone;
	var teacher_id = localStorage.teacher_id;
	$.ajax({
		url: "https://newapp.icandrive.cn/api/teacherInfo.php",
		type: 'post',
		data: {
			token: token,
			teacher_id: teacher_id
		},
		dataType: 'json',
		success: function(data) {
			if(data.status == 200) {
				$('#teacherPhoto').prop('src', data.teacher.teacher_photo);
				$('#teacherPhoto').on('click', function() {
					window.location.href = 'sms://' + data.teacher.phone;
				});
				$('#phone').on('click', function() {
					window.location.href = 'tel://' + data.teacher.phone;
				});
				$('#address').on('click', function() {
					localStorage.gpsj = data.teacher.gps_j;
					localStorage.gpsw = data.teacher.gps_w;
					window.location.href = '../teaching/distence.html';
				});

				localStorage.teacher_name = data.teacher.teacher_name;
				localStorage.teacher_phone = data.teacher.phone;
				$('.name').html(data.teacher.teacher_name);
				$('.content').html(data.teacher.autograph);
				$('#place').val(data.teacher.subject2_place);
				$('#place2').val(data.teacher.subject2_place);
				$('#place3').val(data.teacher.subject3_place);
				$(data.time).each(function() {
					var start = new Date();
					start.setHours(0);
					start.setMinutes(0);
					start.setSeconds(0);
					start.setMilliseconds(0);
					var str = Date.parse(start) / 1000;
					var timestamp = '';
					if(this.clear_date) {
						timestamp = Date.parse(this.clear_date) / 1000;
					} else {
						timestamp = Date.parse(this.order_time) / 1000;
					}
					var data = (timestamp - str) / 3600 / 24;

					if(data >= 0 && data < 1) { //代表今天
						$('#today li').eq(this.time_id).html('<span>关闭</span>');
					}
					if(data >= 1 && data < 2) { //代表明天
						$('#tomorrow li').eq(this.time_id).html('<span>关闭</span>');
					}
					if(data >= 2 && data < 3) { //代表后天
						$('#after li').eq(this.time_id).html('<span>关闭</span>');
					}
				});
			}
			if(data.status == 250) {
				layer.msg("请重新登录");
				setInterval(function() {
					window.location.href = "../login/log_in.html";
				}, 2000)
			}
			if(data.status == 400) {
				layer.msg(data.msg);
				setInterval(function() {
					history.back()
				}, 2000)
			}
		}

	});
	$('#submit').on('click', function() {

		var today = [];
		var tomorrow = [];
		var after = [];
		var subject_type = "";

		$('#today input').each(function() {
			if($(this).prop('checked')) {
				today.push($(this).val());
			}
		})
		$('#tomorrow input').each(function() {
			if($(this).prop('checked')) {
				tomorrow.push($(this).val());
			}
		})
		$('#after input').each(function() {
			if($(this).prop('checked')) {
				after.push($(this).val());
			}
		});

		if(today.length == 0 && tomorrow.length == 0 && after.length == 0) {
			layer.msg('请先选择一个时间段')
			return false;
		}
		var url = window.location.search;
		if(url != '') {

			var type = url.split('?')[1];
			var type2 = type.split('=')[1];

		}

		var teacher_id = localStorage.teacher_id;

		var subject = $('#subject').val();
		var place = $('#place').val();
		var address = '';
		if(type2 == 'date') {
			if(today.length + tomorrow.length + after.length != 1) {
				layer.msg('只能选择一个时间段')
				return false;
			}

			var name = prompt("请输入您的地理位置", "");
			if(name) {
				address = name;
				if(today == '' && tomorrow == '' && after == '') {
					layer.msg('请选择时间')
				} else {
					$.ajax({
						url: 'http:https://newapp.icandrive.cn/api/songkao.php',
						data: {
							token: token,
							address: address,
							teacher_id: teacher_id,
							today: today,
							tomorrow: tomorrow,
							after: after,
							subject: subject
						},
						type: 'post',
						dataType: 'json',
						success: function(data) {
							console.log(data);
							if(data.status == 200) {


								layer.msg('预约成功')

							} else {
								console.log(data);
							}
						},
						error: function() {
							layer.msg('系统繁忙', function() {
								history.go(0)
							}, 1000);
						}
					})
				}

			} else {
				alert('请输入地址')
			}

		} else {

			localStorage.teacher_id = teacher_id;
			localStorage.today = today;
			localStorage.tomorrow = tomorrow;
			localStorage.after = after;
			localStorage.subject = subject;
			localStorage.place = place;
			/*储存科目类型*/
			if($("#subject").val() == "two"){
				localStorage.yue_messages_type ="科目二";
			}if($("#subject").val() == "three"){
				localStorage.yue_messages_type ="科目三";
			}
								console.log(localStorage.yue_messages_type);
			window.location.href = "../info/yuejiao.html"
		}
	})

})/*end $function()*/

function goback() {
	history.back();
}
